<?php

// @formatter:off
/**
 * @file page.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-05-04 01:33:52 +0000 (Sun, 04 May 2014) $
 *
 * $Id: page.php 111 2014-05-04 01:33:52Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/extensions/RelatedComics/controllers/relatedcomics/page.php $
 */
// @formatter:on

class ActionControllerRelatedcomicsPage extends WCActionController {
	//
	// Magic methods.
	public function __construct() {
		parent::__construct();

		$this->_requiredParams[] = "group";
		$this->_requiredParams[] = "comic";

		global $wcGroup;
		global $wcComic;

		$this->_cacheTime = WCViewManager::CacheTimeMax;
		$this->_selector["page"] = "auto";
		$this->_selector["group"] = $wcGroup->id();
		if($wcComic && $wcComic->ok()) {
			$this->_selector["comic"] = $wcComic->id();
		}
	}
	//
	// Public methods.
	public function check() {
		$check = parent::check();

		if($check == WC_HTTPERROR_OK) {
			global $wcProfile;
			global $wcComic;

			if(!$wcProfile->allowedTo(WC_PERM_COMIC_VIEW, WC_PERM_COMIC_LIST)) {
				global $errno;
				global $error;

				$check = WC_HTTPERROR_FORBIDDEN;
				$errno = $check;
				$error = "You don't have enough priviliges";
			}
			if($check == WC_HTTPERROR_OK && (!$wcComic || !$wcComic->ok())) {
				global $errno;
				global $error;

				$check = WC_HTTPERROR_INTERNAL_SERVER_ERROR;
				$errno = $check;
				$error = "There seems to be a problem with parameters";
			}
		}

		return $check;
	}
	//
	// Protected methods.
	protected function subRun() {
		$ok = true;

		//
		// Loading global pointers.
		global $wcGroup;
		global $wcComic;
		global $wcCollection;
		global $wcsDefaults;
		global $wcActiveTagsHolder;
		global $wcProfile;

		$page = isset($_REQUEST["page"]) ? $_REQUEST["page"] : 1;
		$minCmcInPage = $wcsDefaults["max-per-page"]["tag"] * ($page - 1);
		$maxCmcInPage = $minCmcInPage + $wcsDefaults["max-per-page"]["tag"];

		$this->_manager->PATH = "Comics related to {$wcComic->name()}";

		$tags = WCTagsHolder::I()->tagsOfComic($wcComic);
		//
		// Comic information.
		// @{
		$this->_manager->info_id = $wcComic->id();
		$this->_manager->info_name = $wcComic->name();
		$this->_manager->info_directory = $wcComic->directory()->name();
		if($wcProfile->granted(WC_PERM_DIRECTORY_VIEW)) {
			$this->_manager->info_directory_link = $wcComic->directory;
		}
		if($wcCollection) {
			$this->_manager->info_collections = $wcCollection->collectionPath();
		}
		$info_tags = array();
		foreach($tags as $tag) {
			$aux = array(
				"name" => $tag->name,
				"link" => $tag->id
			);

			if($wcActiveTagsHolder->isActive($tag->name)) {
				$aux["special"] = true;
			}

			$info_tags[$tag->name] = $aux;
		}
		ksort($info_tags);
		$this->_manager->info_tags = $info_tags;
		// @}

		$comicIds = array();
		foreach($tags as $tag) {
			foreach($tag->comics() as $id) {
				@$comicIds[$id] ++;
			}
		}
		if(isset($comicIds[$wcComic->id])) {
			unset($comicIds[$wcComic->id]);
		}
		arsort($comicIds);

		$comics = array();
		$comicCount = 0;
		$mostComics = array();
		$mostComicCount = 0;
		foreach($comicIds as $id => $count) {
			$comic = WCComicsHolder::I()->item($id);

			if($comic && $comic->ok() && $comic->group == $wcGroup->id) {
				if($comicCount >= $minCmcInPage && $comicCount < $maxCmcInPage) {
					$comics[] = WCFVBoxes::GetComicPreviewAssigns($comic);
				}
				if($mostComicCount < 5) {
					$mostComics[] = WCFVBoxes::GetComicPreviewAssigns($comic);
				}
				$comicCount++;
				$mostComicCount++;
			}
		}

		$this->_manager->comics = $comics;
		$this->_manager->most_comics = $mostComics;
		if($comics) {
			$this->_manager->has_comics = true;
		}

		WCFVPager::GetAssignsIntoManager(ceil($comicCount / $wcsDefaults["max-per-page"]["tag"]));

		return $ok;
	}
}
